<?php

namespace app\manage\controller;


use app\common\validate\system\SystemManagerValid;
use app\manage\loader\ManageModel;
use app\manage\loader\ManageServices;
use app\manage\services\SystemService;
use think\Exception;

/**
 *  系统管理员
 */
class Manager extends BaseApi
{

    /**
     * 管理员登录
     * @return void
     */
    public function login(): void
    {
        $postData = input('post.');
        try {

            validate(SystemManagerValid::class)->scene('login')->check($postData);
            $authData = ManageServices::Manager()->login($postData['username'], $postData['passwd']);
            api_result(API_SUCCESS, '登录成功', $authData);
        } catch (Exception $e) {
            api_result($e);
        }
    }

    /**
     * 格式化where条件
     * @param $params
     * @return array
     */
    protected function formatWhere($params): array
    {
        $map = [];

        if (!empty($params['keyword'])) {
            $keyword = trim($params['keyword']);
            if (isMobile($keyword)) {
                $map[] = ['mobile', '=', $keyword];
            } else {
                $map[] = ['title|contact', 'like', '%' . $keyword . '%'];
            }
        }
        return $map;
    }

    /**
     * 管理员列表
     * @return void
     */
    public function page(): void
    {
        $getData = input('get.');
        try {
            $option = $this->formatOption($getData);
            $option['where'] = $this->formatWhere($getData);
            $result = ManageServices::Manager()->page($option);
            api_result(API_SUCCESS, '登录成功', $result);
        } catch (Exception $e) {
            api_result($e);
        }
    }
    /**
     * 管理员列表
     * @return void
     */
    public function list(): void
    {
        $getData = input('get.');
        try {
            $option[] = [];
            if (!empty($getData['keyword'])) {
                $keyword = trim($getData['keyword']);
                $option['where'][] = ['username', 'like', '%' . $keyword . '%'];
            }
            $result = ManageServices::Manager()->list($option);
            api_result(API_SUCCESS, '请求成功', $result);
        } catch (Exception $e) {
            api_result($e);
        }
    }

    /**
     * 管理员info
     * @return void
     */
    public function info(): void
    {
        $getData = input('get.');
        try {
            if (empty($getData['id'])){
                throw  new Exception('未找到该用户',API_ERROR);
            }
            $option['where'] = ['id' => $getData['id']];
            $result = ManageServices::Manager()->info($option);
            api_result(API_SUCCESS, '请求成功', $result);
        } catch (Exception $e) {
            api_result($e);
        }
    }


    /**
     * 添加管理员
     * @return void
     */
    public function save(): void
    {
        try {
            $postData = input('post.');
            $scene = empty($postData['id']) ? 'add' : 'edit';
            validate(SystemManagerValid::class)->scene($scene)->check($postData);
            $result = ManageServices::Manager()->save($postData, $scene);
            api_result(API_SUCCESS, ($scene === 'add' ? '添加' : '修改') . '成功', ['id' => $result]);
        } catch (Exception $e) {
            api_result($e);
        }
    }

    /**
     * 重置密码
     * @return void
     */
    public function reset_password(): void
    {
        $postData = input('post.');
        try {

            validate(SystemManagerValid::class)->scene('reset_passwd')->check($postData);
            ManageServices::Manager()->resetPassword($postData['id'], $postData['passwd']);
            api_result(API_SUCCESS, '操作成功');
        } catch (Exception $e) {
            api_result($e);
        }
    }

    /**
     * 编辑状态
     * @return void
     */
    public function edit_status(): void
    {
        $postData = input('post.');
        try {

            validate(SystemManagerValid::class)->scene('edit_status')->check($postData);
            ManageServices::Manager()->editStatus($postData['id'], $postData['status']);
            api_result(API_SUCCESS, '操作成功');
        } catch (Exception $e) {
            api_result($e);
        }
    }

    /**
     * 菜单用户路由
     */
    public function group_menu(){
        try {
            $list = ManageServices::SystemMenu()->userInterfaceList($this->userInfo['group_id']);
            api_result(API_SUCCESS, '请求成功',$list);
        } catch (Exception $exception) {
            api_result($exception);
        }
    }


    /**
     * 登录用户权限菜单
     */
    public function get_action(){
        try {
            $list =   ManageServices::Manager()->getAction($this->userInfo['group_id']);
            api_result(API_SUCCESS, '请求成功',$list);
        } catch (Exception $exception) {
            api_result($exception);
        }
    }
}
